<?php

class Application_Form_ParkplatzAnlegenForm extends Zend_Form
{
	protected $_parkplatzNr;
	protected $_anzahl;
	protected $_standort;
	protected $_beschreibung;
	protected $_kalenderName;
	
	
	public function __construct($options = null, $parkplatzNr = '', $anzahl = '', $standort = '', $beschreibung = '')
    {
        $this->_parkplatzNr = $parkplatzNr;
        $this->_anzahl = $anzahl;
        $this->_standort = $standort;
        $this->_beschreibung = $beschreibung;
        parent::__construct($options);
    }
    
    public function init()
    {
    	
    	/* Form Elements & Other Definitions Here ... */
    	$decorator = new My_Decorator_SimpleInput();
    	$decorator1 = new My_Decorator_SimpleInput1();

    	// Set the method for the display form to POST
    	$this->setName('Parkplatz erstellen');
        $this->setMethod('post');
        
        // Parkplaetze
        $anzahl  = new Zend_Form_Element('anzahl', array(
        'label'      => 'Parkplaetze',
        'value'      => $this->_anzahl,
        'validators' => array(
                array('validator' => 'int')
                )
    	));
    	
        $anzahl
        ->setRequired()
        ->addErrorMessage('Eingabe fehlt oder falsch');
        $anzahl
        ->clearDecorators()
        ->addDecorator($decorator)
        ->addDecorator('Errors');
    	
    	//alt
    	/*
        $this->addElement('text', 'anzahl', array(
            'label'      => 'Parkplaetze:',
            'required'   => true,
            'decorators' => array($decorator),
            'validators' => array(
                array('validator' => 'int')
            )
        ));
        */
        
        // Standort
        
        $standort = new Zend_Form_Element('standort', array(
        'label'      => 'Standort',
        'value'      => $this->_standort,
        'filters'    => array('StringTrim'),
        'validators' => array(
                array('validator' => 'StringLength', 'options' => array(1, 50))
            )
    	));
    	
    	$standort
    	->setRequired()
    	->addErrorMessage('Eingabe fehlt');
    	$standort
    	->clearDecorators()
    	->addDecorator($decorator)
    	->addDecorator('Errors');
        
        //alt
        /*
        $this->addElement('text', 'standort', array(
            'label'      => 'Standort:',
            'required'   => true,
            'decorators' => array($decorator),
         //   'filters'    => array('StringTrim'),
            'validators' => array(
                array('validator' => 'StringLength', 'options' => array(1, 50))
                )
        ));
        */
        
        // Beschreibung
        
    	$beschreibung = new Zend_Form_Element('beschreibung', array(
        'label'      => 'Beschreibung',
        'value'      => $this->_beschreibung,
        'filters'    => array('StringTrim'),
        'validators' => array(
                array('validator' => 'StringLength', 'options' => array(1, 2048))
            )
    	));
    	
    	$beschreibung
    	->clearDecorators()
    	->addDecorator($decorator)
    	->addDecorator('Errors');
        
    	/*
        // Vorsicht, Teständerung
        $this->addElement('textarea', 'beschreibung', array(
            'label'      => 'Beschreibung:',
            'required'   => false,
            'decorators' => array($decorator1),
        //    'filters'    => array('StringTrim'),
            'validators' => array(
                array('validator' => 'StringLength', 'options' => array(1, 2048))
            )
        ));
        */

   		// Submit button
   		$submit = new Zend_Form_Element_Submit('Speichern', array('class' => 'button'));
		$submit->setLabel('Speichern');
    	$form = new Zend_Form();
    	$_kaledernName = '0';
    	
		$this->addElements(array($anzahl, $standort, $beschreibung, $submit));
		
		//alt
		/*
		$this->addElement('submit', 'submit', array('label' => 'Speichern', 'class' => 'button'));
		*/
    
    }
}


class My_Decorator_SimpleInput1 extends Zend_Form_Decorator_Abstract{
	
	protected $_format = '<p><label for="%s">%s</label><textarea id="beschreibung" name="beschreibung" rows="8" cols="40"></textarea></p>';
     
	public function render($content){
		
		$element = $this->getElement();
		$name    = htmlentities($element->getFullyQualifiedName());
		$label   = htmlentities($element->getLabel());
		$id      = htmlentities($element->getId());
		$value   = htmlentities($element->getValue());
     
		$markup  = sprintf($this->_format, $name, $label, $id, $name, $value);
		return $markup;
	}
}
    
    
class My_Decorator_SimpleInput extends Zend_Form_Decorator_Abstract{
	
	protected $_format = '<p><label for="%s">%s</label><input id="%s" name="%s" type="text" value="%s"/></p>';
	
	//Test
	public function buildErrors(){
		
		$element  = $this->getElement();
		$parkplatzNr = $element->getParkplatzNr();
		
		if (empty($parkplatzNr))
		{
			return 'Error';
		}
        	
        return '<div class="errors">' . $element->getView()->formErrors($messages) . '</div>';
	}
	//
    
	public function render($content){
		
		$element = $this->getElement();
		$name    = htmlentities($element->getFullyQualifiedName());
		$label   = htmlentities($element->getLabel());
		$id      = htmlentities($element->getId());
		$value   = htmlentities($element->getValue());
     
		$markup  = sprintf($this->_format, $name, $label, $id, $name, $value);
		
		return $markup;
	}
}
